package xingkong.core;

import java.util.Date;
import java.util.List;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import xingkong.tools.ResponseContent;
import xingkong.tools.StaticFields;
/**
 * 积分
 * @author befory
 *
 */
public class IntegralController extends Controller{
	/**
	 * 到积分列表
	 */
	public void tojifenrecord(){
		renderJsp("/WEB-INF/content/jifenrecord.jsp");
	}
	/**
	 * 到积分列表
	 */
	public void toAddJinfen(){
		renderJsp("/WEB-INF/content/addjifen.jsp");
	}
	
	/**
	 * 获取积分列表
	 * 返回积分列表及分页参数
	 */
	public void getIntegralList(){
		int page = getParaToInt("page");//当前页码
		if(page ==0) page =1;
		int size = getParaToInt("size");
		if(size ==0) size = StaticFields.DEFAULT_SIZE;
		Record record = (Record) getSession().getAttribute(StaticFields.CURRENTUSER);
		List<Record> list = null;
		long count=0;
		long total=0L;
		if("1".equals(record.get("type"))){
			String name = getPara("name");
			if(StringUtils.isEmpty(name)){
				list = Db.find("select i.id,i.record,i.type,i.create_time,u.login_name from tb_integral_record i left join tb_user u on i.user_id = u.id limit ?,?",(page-1)*size,size);
				count = Db.queryLong("select count(1) from tb_integral_record i left join tb_user u on i.user_id = u.id");
			}else {
				list = Db.find("select i.id,i.record,i.type,i.create_time,u.login_name from tb_integral_record i left join tb_user u on i.user_id = u.id where u.login_name like ? limit ?,?","%"+name+"%",(page-1)*size,size);
				count = Db.queryLong("select count(1) from tb_integral_record i left join tb_user u on i.user_id = u.id where u.login_name like ?","%"+name+"%");
			}
		}else {
			list = Db.find("select i.id,i.record,i.type,i.create_time,u.login_name from tb_integral_record i left join tb_user u on i.user_id = u.id where i.user_id=? limit ?,?",record.getStr("id"),(page-1)*size,size);
			count = Db.queryLong("select count(1) from tb_integral_record i left join tb_user u on i.user_id = u.id where i.user_id=?",record.getStr("id"));
		}
		if(count == 0){
			total = 1;
		}else {
			total = count % size == 0 ? count / size : (count / size +1);
		}
		renderJson(new ResponseContent(StaticFields.CORRECT, "", new Record().set("page", page).set("size", size).set("count", count).set("list", list).set("total", total)));
	}
	/**
	 * 充值积分
	 * @throws Exception
	 */
	@Before(Tx.class)
	public void addIntegral() throws Exception{
		String username = getPara("login_name");
		if(StringUtils.isEmpty(username)){
			renderJson(new ResponseContent(StaticFields.WRONG, "用户名不能为空", null));
			return;
		}
		Record user = this.getUser(username);
		if(user == null){
			renderJson(new ResponseContent(StaticFields.WRONG, "没有该用户", null));
			return;
		}
		int integral = getParaToInt("integral");
		if(integral <0){
			renderJson(new ResponseContent(StaticFields.WRONG, "积分输入有误", null));
			return;
		}
		boolean f = Db.save("tb_integral_record", new Record().set("record", integral).set("type", 1).set("user_id", user.get("id")).set("create_time", new Date()));
		if(!f){
			renderJson(new ResponseContent(StaticFields.WRONG, "添加数据异常", null));
			throw new Exception();
		}
		int i = Db.update("UPDATE `xingkong`.`tb_integral` SET `integral` = integral + ? WHERE `user_id` = ?",integral,user.get("id"));
		if(i<1){
			renderJson(new ResponseContent(StaticFields.WRONG, "添加数据异常", null));
			throw new Exception();
		}
		renderJson(new ResponseContent(StaticFields.CORRECT, "积分充值成功！", null));
	}
	/**
	 * 根据用户名查找用户
	 * @param username 用户名
	 * @return 用户
	 */
	private Record getUser(String username){
		return Db.findFirst("select * from tb_user u where u.login_name=?",username);
	}
}
